import {
  CashModel
}from '../../../../models/cash'
const App =  getApp();

const cashModel = new CashModel()

Page({

    /**
     * 页面的初始数据
     */
    data: {
      active: 0,
      bank:[],
      alipay:[],
      delBtnWidth: 180, //删除按钮宽度单位（rpx）
      statusBarHeight: App.globalData.statusBarHeight,
      isTabbar:false
    },

    tapToAlipay:function(event){
        wx.navigateTo({
          url: '/pages/manager/commision/add-alipay/index',
        })
    },
  
    tapToCard:function(event){
      wx.navigateTo({
        url: '/pages/manager/commision/add-card/index',
      })
    },

    //设置默认到账方式
    wayAccount:function(e){
      if (e.currentTarget.dataset.def == 1){
        // wx.redirectTo({
        //   url: '/pages/manager/commision/take-out/index?active='+this.data.active
        // })
        wx.navigateBack({
          delta: 1
        })
      }else{
        const params = {
          id: e.currentTarget.dataset.id,
          isDef: !e.currentTarget.dataset.def
        }

        cashModel.wayAccount(params).then(res => {
          // wx.redirectTo({
          //   url: '/pages/manager/commision/take-out/index?active='+this.data.active
          // })
          wx.navigateBack({
            delta: 1
          })
        }).catch();
      }
      
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {  
        if (options.active) {
          this.setData({
            active: options.active
          })
        }
        this.initEleWidth();

    },  
    touchS: function (e) {
        if (e.touches.length == 1) {
          this.setData({
            //设置触摸起始点水平方向位置
            startX: e.touches[0].clientX
          });
        }
      },
      touchM: function (e) {
        const dataList = e.currentTarget.dataset.list;
        if (e.touches.length == 1) {
          //手指移动时水平方向位置
          var moveX = e.touches[0].clientX;
          //手指起始点位置与移动期间的差值
          var disX = this.data.startX - moveX;
          var delBtnWidth = this.data.delBtnWidth;
          var txtStyle = "";
          if (disX == 0 || disX < 0) { //如果移动距离小于等于0，文本层位置不变
            txtStyle = "left:0px";
          } else if (disX > 0) { //移动距离大于0，文本层left值等于手指移动距离
            txtStyle = "left:-" + disX + "px";
            if (disX >= delBtnWidth) {
              //控制手指移动距离最大值为删除按钮的宽度
              txtStyle = "left:-" + delBtnWidth + "px";
            }
          }
          //获取手指触摸的是哪一项
          var index = e.currentTarget.dataset.index;
          if(dataList == 1){
            var list = this.data.alipay;
          }else{
            var list = this.data.bank;
          }
          
          if (index >= 0) {
            list[index].txtStyle = txtStyle;
            //更新列表的状态
            if (dataList == 1) {
              this.setData({
                alipay: list
              });
            } else {
              this.setData({
                bank: list
              });
            }
          }
        }
      },

      touchE: function (e) {
        const dataList = e.currentTarget.dataset.list;
        if (e.changedTouches.length == 1) {
          //手指移动结束后水平位置
          var endX = e.changedTouches[0].clientX;
          //触摸开始与结束，手指移动的距离
          var disX = this.data.startX - endX;
          var delBtnWidth = this.data.delBtnWidth;
          //如果距离小于删除按钮的1/2，不显示删除按钮
          var txtStyle = disX > delBtnWidth / 2 ? "left:-" + delBtnWidth + "px" : "left:0px";
          //获取手指触摸的是哪一项
          var index = e.currentTarget.dataset.index;
          if (dataList == 1) {
            var list = this.data.alipay;
          } else {
            var list = this.data.bank;
          }
          if (index >= 0) {
            list[index].txtStyle = txtStyle;
            //更新列表的状态
            if (dataList == 1) {
              this.setData({
                alipay: list
              });
            } else {
              this.setData({
                bank: list
              });
            }
          }
        }
      },
      //获取元素自适应后的实际宽度
      getEleWidth: function (w) {
        var real = 0;
        try {
          var res = wx.getSystemInfoSync().windowWidth;
          var scale = (750 / 2) / (w / 2); //以宽度750px设计稿做宽度的自适应
          // console.log(scale);
          real = Math.floor(res / scale);
          return real;
        } catch (e) {
          return false;
          // Do something when catch error
        }
      },
      initEleWidth: function () {
        var delBtnWidth = this.getEleWidth(this.data.delBtnWidth);
        this.setData({
          delBtnWidth: delBtnWidth
        });
      },

      delItem: function (e) {
          const dataList = e.currentTarget.dataset.list;
          const id = e.currentTarget.dataset.id;
          const self = this;
          //获取列表中要删除项的下标
          var index = e.target.dataset.index;
          if (dataList == 1) {
            var list = self.data.alipay;
          } else {
            var list = self.data.bank;
          }
          const params = {
            id:id
          }
          cashModel.delBank(params).then(res => {
            //更新列表的状态
            list.splice(index,1);
            if (dataList == 1) {
              self.setData({
                alipay:list
              })
            } else {
              self.setData({
                bank: list
              })
            }
            self.initData();
            wx.showToast({
              title: "删除成功",
              duration: 1000,
              icon: 'none'
            })
          }).catch();
          //移除列表中下标为index的项
          
        },
  
  
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
  
    },
  
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      this.initData()
    },
  

    initData:function(){
      cashModel.getCashModeList()
      .then(res=>{
        if(res.data.bank.length > 0){
          for(var i in res.data.bank){
            res.data.bank[i].cardNumber = res.data.bank[i].cardNo.replace(/(.{4})/g, "$1 ")
          }
        }
        this.setData({
          bank:res.data.bank,
          alipay:res.data.Alipay
        })
      })

    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
  
    },
  
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
  
    },
  
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
  
    },
  
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
  
    },

  })